<template>
  <div class="search">
    <div class="search-input-wrapper">
      <searchInput v-model="query"></searchInput>
    </div>
    <scroll
      ref="scrollRef"
      class="search-content"
      v-show="!query"
    >
      <div>
        <div class="hot-keys">
          <h1 class="title">热门搜索</h1>
          <ul>
            <li @click="addQuery(item.key)" class="item" v-for="item in hotKeys" :key="item.id">
              <span>{{item.key}}</span>
            </li>
          </ul>
        </div>
        <div class="search-history" v-show="searchHistory.length">
          <h1 class="title">
            <span class="text">搜索历史</span>
            <span class="clear" @click="showConfirm">
              <i class="icon-clear"></i>
            </span>
          </h1>
          <confirm
            ref="confirmRef"
            text="是否清空所有搜索历史"
            confirm-btn-text="清空"
            @confirm="clearSearch"
          ></confirm>
          <searchList
            :searches="searchHistory"
            @select="addQuery"
            @delete="deleteSearch"
          ></searchList>
        </div>
      </div>
    </scroll>
    <div class="search-result" v-show="query">
      <Suggest
        @select-singer="selectSinger"
        @select-song="selectSong"
        :query="query"
      ></Suggest>
    </div>
       <router-view v-slot="{Component}">
      <transition appear name="slide">
        <component :is="Component" :data="selectedSinger"></component>
      </transition>
    </router-view>
  </div>
</template>

<script>
import searchInput from '@/components/search/search-input.vue';
import { computed, ref, watch, nextTick } from 'vue';
import Suggest from '../components/search/suggest.vue';
import { getHotKeys } from '../service/search';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
import { SINGER_KEY } from '@/assets/js/constant.js';
import searchList from '@/components/base/search-list/search-list.vue';
import useSearchHistory from '../components/search/use-search-history';
import scroll from '../components/wrap-scroll';
import confirm from '../components/base/confirm/confirm.vue';

export default {
  name: 'search',
  components: {
    searchInput,
    Suggest,
    searchList,
    scroll,
    confirm
  },
  setup() {
    const query = ref('');
    const hotKeys = ref([]);
    const selectedSinger = ref(null);
    const scrollRef = ref(null);
    const confirmRef = ref(null);

    const store = useStore();
    const router = useRouter();

    const searchHistory = computed(() => store.state.searchHistory);
    const { saveSearch, deleteSearch, clearSearch } = useSearchHistory();
    getHotKeys().then(result => {
      hotKeys.value = result.hotKeys;
    });

    // 滚动bug处理,在页面渲染时执行refresh
    watch(query, async (val) => {
      if (!val) {
        await nextTick();
        refreshScroll();
      }
    });

    function refreshScroll() {
      scrollRef.value.scroll.refresh();
    }

    function addQuery(s) {
      query.value = s;
    }

    function selectSong(song) {
      saveSearch(query.value);
      store.dispatch('addSong', song);
    }

    function selectSinger(singer) {
      saveSearch(query.value);
      selectedSinger.value = singer;
      cacheSinger(singer);
      router.push({
        path: `/singer/${singer.mid}`
      });
    }
    // 本地缓存
    function cacheSinger(singer) {
      const singers = JSON.stringify(singer);
      sessionStorage.setItem(SINGER_KEY, singers);
    }

    function showConfirm() {
      confirmRef.value.show();
    }

    return {
      clearSearch,
      confirmRef,
      showConfirm,
      scrollRef,
      selectSong,
      selectSinger,
      query,
      hotKeys,
      addQuery,
      selectedSinger,
      searchHistory,
      deleteSearch
    };
  }
};
</script>

<style lang="scss" scoped>
  .search {
    position: fixed;
    width: 100%;
    top: 88px;
    bottom: 0;
    display: flex;
    flex-direction: column;
    .search-input-wrapper {
      margin: 20px;
    }
    .search-content {
      flex: 1;
      overflow: hidden;
      .hot-keys {
        margin: 0 20px 20px 20px;
        .title {
          margin-bottom: 20px;
          font-size: $font-size-medium;
          color: $color-text-l;
        }
        .item {
          display: inline-block;
          padding: 5px 10px;
          margin: 0 20px 10px 0;
          border-radius: 6px;
          background: $color-highlight-background;
          font-size: $font-size-medium;
          color: $color-text-d;
        }
      }
      .search-history {
        position: relative;
        margin: 0 20px;
        .title {
          display: flex;
          align-items: center;
          height: 40px;
          font-size: $font-size-medium;
          color: $color-text-l;
          .text {
            flex: 1;
          }
          .clear {
            @include extend-click();
            .icon-clear {
              font-size: $font-size-medium;
              color: $color-text-d;
            }
          }
        }
      }
    }
    .search-result {
      flex: 1;
      overflow: hidden;
    }
  }
</style>
